<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/12
 * Time: 17:30
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Freight extends Base
{
    /**
     * 模版展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $fre_city_name = input('fre_city_name'); // 收货地
            $fre_type = input('fre_type'); // 类型
            $fre_status = input('fre_status'); // 状态
            $fre_name = input('fre_name'); // 名称
            $where = [];
            
            if(isset($fre_status) && $fre_status != ""){
                $where['fre_status'] = $fre_status;
            }
            if(isset($fre_type) && $fre_type != ""){
                $where['fre_type'] = $fre_type;
            }
            if(isset($fre_name) && $fre_name != ''){
                $where['fre_name'] = ['like', '%' . $fre_name . '%'];
            }
            if(isset($fre_city_name) && $fre_city_name != ''){
                $where['fre_city_name'] = ['like', '%' . $fre_city_name . '%'];
            }
        
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('fre')->where($where)->count();// 获取总条数
            $lists = Db::name('fre')
                ->where($where)
                ->field('fre_id, fre_name, fre_city_name, fre_type, fre_status')
                ->order('fre_time desc')
                ->page($NowPage, $limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加运费模版
     * @return mixed|\think\response\Json
     */
    public function addFre(){
        if(Request::instance()->isPost()){
            $fre_name = trim(input('fre_name'));
            $fre_status = input('fre_status');
            $fre_type = input('fre_type');
            $fre_city_id = input('fre_city_id');
            $fre_city_name = Db::table('up_area')
                ->where('district_id', 'IN', $fre_city_id)
                ->column('district');
            $instrt = [
                'fre_name' => $fre_name,
                'fre_city_id' => $fre_city_id,
                'fre_city_name' => implode(',', $fre_city_name),
                'fre_type' => $fre_type,
                'fre_status' => $fre_status,
                'fre_time' => date('Y-m-d H:i:s'),
                'fre_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_fre')->insert($instrt);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $province = Db::table('up_area')->where('level', 2)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch('freight/addfre');
    }
    
    /**
     * 编辑运费模版
     * @return mixed|\think\response\Json
     */
    public function editFre(){
        $fre_id = input('fre_id');
        if(Request::instance()->isPost()){
            $fre_type = input('fre_type');
            $fre_city_id = input('fre_city_id');
            $fre_city_name = Db::table('up_area')
                ->where('district_id', 'IN', $fre_city_id)
                ->column('district');
            
            $update = [
                'fre_city_id' => $fre_city_id,
                'fre_city_name' => implode(',', $fre_city_name),
                'fre_type' => $fre_type,
                'fre_update_time' => date('Y-m-d H:i:s')
            ];
            
            $res = Db::table('up_fre')->where('fre_id', $fre_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        // 城市
        $province = Db::table('up_area')->where('level', 2)->field('district_id, district')->select();
        // 模版
        $freight = Db::table('up_fre')->where('fre_id', $fre_id)->field('fre_type, fre_city_id')->find();
        $this->assign([
            'province' => $province,
            'freight' => $freight,
            'fre_id' => $fre_id,
            'id' => 1002
        ]);
        return $this->fetch('freight/editFre');
    }
    
    /**
     * 模版状态
     * @return \think\response\Json
     */
    public function freChange(){
        $fre_id = input('id');
        $fre_status = Db::table('up_fre')->where('fre_id', $fre_id)->value('fre_status');
        if($fre_status == 1){
            // 如果禁用，则需判断该模版有没有被使用
            $is_have = Db::table('up_goods')
                ->where('goods_freight', $fre_id)
                ->where('goods_del', 1)
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '该运费模版已被商品使用，不可禁用']);
            }
            $status = 2;
        }else{
            $status = 1;
        }
        $res = Db::table('up_fre')->where('fre_id', $fre_id)->setField('fre_status', $status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除模版
     * @return \think\response\Json
     */
    public function freDel(){
        $fre_id = input('id');
        $is_have = Db::table('up_goods')
            ->where('goods_freight', $fre_id)
            ->where('goods_del', 1)
            ->find();
        if($is_have){
            return json(['code' => 100, 'msg' => '该运费模版已被商品使用，不可删除']);
        }
        $res = Db::table('up_fre')->delete($fre_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}